package com.demo.iodemo;

import java.io.BufferedReader;
import java.io.FileInputStream;
import java.io.FileReader;
import java.io.IOException;
import java.io.InputStreamReader;
import java.io.Reader;

public class FileReadDemo {

	public static void main(String[] args) throws IOException {
		/*String fileName = FileReadDemo.class.getClass().getResource("/")
				.getFile()
				+ "/nio-data.txt";*/
		String fileName = "d://locationData.txt";
		readFileByFileInputStream(fileName);
		//System.getProperties().list(System.out);;
	}

	/**
	 * 使用java.io.FileReader类读取文件
	 * @param fileName
	 * @throws IOException
	 */
	public static void readFileByFileReader(String fileName) throws IOException {
		FileReader reader = new FileReader(fileName);
		readFileByIOReader(reader);
		reader.close();
	}
	
	/**
	 * 
	 * @param fileName
	 * @throws IOException
	 */
	public static void readFileByFileInputStream(String fileName) throws IOException {
		FileInputStream fis = new FileInputStream(fileName);
		String charsetName = System.getProperty("sun.jnu.encoding", "UTF-8");
		InputStreamReader inputStreamReader = new InputStreamReader(fis, charsetName);
		readFileByIOReader(inputStreamReader);
		inputStreamReader.close();
		fis.close();
	}
	
	/**
	 * 文件读取过程中，不论用java.io.FileReader还是java.io.FileInputStream,
	 * 最终都要转换成java.io.Reader去读取文件内容。
	 * @param reader
	 * @throws IOException 
	 */
	private static void readFileByIOReader(Reader reader) throws IOException{
		BufferedReader bufferedReader = new BufferedReader(reader, 1024);//这里可以设置缓存大小,默认为8192
		String tempString = null;
		while ((tempString = bufferedReader.readLine()) != null){
			//输出
			System.out.println(tempString);
			tempString = null;
		}
		bufferedReader.close();
	}
	

}
